package com.yzk.aigy.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzk.aigy.common.Result;
import com.yzk.aigy.common.VodTemplate;
import com.yzk.aigy.model.system.SysMovie;
import com.yzk.aigy.model.vo.SysMovieQueryVo;
import com.yzk.aigy.model.vo.UpdateMovieVo;
import com.yzk.aigy.service.SysMovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "影视管理控制器")
@RequiredArgsConstructor
@RequestMapping("/admin/system/sysMovie")
public class SysMovieController {

    private final SysMovieService sysMovieService;
    private final VodTemplate vodTemplate;

    @ApiOperation("获取全部影视列表")
    @GetMapping("/findAll")
    public Result<List<SysMovie>> findAll() {
        return Result.ok(sysMovieService.list());
    }

    @ApiOperation("分页查询影视")
    @PostMapping("/page")
    public Result<IPage<SysMovie>> findMovieByPageQuery(@RequestBody SysMovieQueryVo sysMovieQueryVo) {
        IPage<SysMovie> page = new Page<>(sysMovieQueryVo.getPageNum(), sysMovieQueryVo.getPageSize());
        Wrapper<SysMovie> wrapper = Wrappers.<SysMovie>lambdaQuery()
                .like(StringUtils.hasText(sysMovieQueryVo.getName()), SysMovie::getName, sysMovieQueryVo.getName())
                .eq(sysMovieQueryVo.getCid() != null, SysMovie::getCid, sysMovieQueryVo.getCid())
                .orderByDesc(SysMovie::getId);
        return Result.ok(sysMovieService.page(page, wrapper));
    }

    @ApiOperation("添加影视")
    @PostMapping("/addMovie")
    public Result<Object> addMovie(@RequestBody SysMovie sysMovie) {
        return sysMovieService.save(sysMovie) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID查询影视")
    @GetMapping("/findMovieById/{id}")
    public Result<SysMovie> findMovieById(@PathVariable Long id) {
        return Result.ok(sysMovieService.getById(id));
    }

    @ApiOperation("修改影视")
    @PostMapping("/updateMovie")
    public Result<Object> updateRole(@RequestBody UpdateMovieVo updateMovieVo) {
        return sysMovieService.updateMovie(updateMovieVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID移除影视")
    @DeleteMapping("/removeMovie/{id}")
    public Result<Object> removeMovie(@PathVariable Long id) {
        return sysMovieService.removeMovieById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation("批量删除影视")
    @DeleteMapping("/removeMovieByIds")
    public Result<Object> removeMovieByIds(@RequestBody List<Long> ids) {
        return sysMovieService.removeMovieByIds(ids) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID获取播放凭证")
    @GetMapping("/getPlayAuth/{id}")
    public Result<Map<String, Object>> getPlayAuth(@PathVariable Long id) throws Exception {

        // 1.根据id获取movie
        SysMovie sysMovie = sysMovieService.getById(id);

        //2.根据movie获取播放id
        String playId = sysMovie.getPlayId();

        //3.获取 封面
        String image = sysMovie.getImage();

        //4.根据播放id获取auth
        String playAuth = vodTemplate.getVideoPlayAuth(playId).getPlayAuth();

        Map<String, Object> map = new HashMap<>();
        map.put("image", image);
        map.put("playId", playId);
        map.put("playAuth", playAuth);

        return Result.ok(map);
    }

}
